package de.g18.scavengerhunt.overlay;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 *  This overlay draws the battlezone on the map
 */
public class CircleOverlay extends Overlay {

    Context context;
    GeoPoint mGeoPoint = null;
    int mRadius;

    public CircleOverlay(Context _context) {
    	context = _context;
    }
    
	 public CircleOverlay(Context _context, GeoPoint geoPoint, int radius) {
	        context = _context;
	        setGeoPoint(geoPoint);
	        setRadius(radius);
	 }
	
	 @Override
	 public void draw(Canvas canvas, MapView mapView, boolean shadow) {
	     super.draw(canvas, mapView, shadow); 
	
	     if(mGeoPoint == null || mRadius == 0 || shadow) return;
	
	     Projection projection = mapView.getProjection();
	     Point pt = new Point();
	     projection.toPixels(mGeoPoint ,pt);
	    	    
	     float circleRadius = (float) (mapView.getProjection().metersToEquatorPixels(mRadius) * (1 / Math.cos(Math.toRadians(mGeoPoint.getLatitudeE6() / 1E6))));
	
	     Paint innerCirclePaint;
	     innerCirclePaint = new Paint();
	     innerCirclePaint.setColor(Color.GREEN);
	     innerCirclePaint.setAlpha(70);
	     innerCirclePaint.setAntiAlias(true);
	     innerCirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
	
	     Log.d(this.getClass().getSimpleName(), "Drawing CircleOverlay: x = " + pt.x + "|y = " + pt.y + "|meters = " + mRadius + "|circleRadius = " + circleRadius);
	     
	     canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);
	}
 
	 public void setGeoPoint(GeoPoint geoPoint) {
		 this.mGeoPoint = geoPoint;
		 Log.d(this.getClass().getSimpleName(), "setGeoPoint " + "lat=" + geoPoint.getLatitudeE6() + "|lng=" + geoPoint.getLongitudeE6());
	 }
 
	public void setRadius(int radius) {
		this.mRadius = radius;
		Log.d(this.getClass().getSimpleName(), "setRadius " + radius);
	}
}
